import wepy from 'wepy'

export default class extends wepy.mixin {
  data = {
    goods_id: '',
    detailList: [],
    address: null
  }

  config = {
  }
  computed = {
    chosenAdd() {
      if (this.address) return this.address.provinceName + this.address.cityName + this.address.countyName + this.address.detailInfo
    },
    cartNum() {
      let num = 0
      this.$parent.globalData.cart.forEach(x => {
        if (x.isCheck) {
          num += x.count
        }
      })
      return num
    }
  }
  methods = {
    imgPreview(current) {
      wepy.previewImage({
        urls: this.detailList.pics.map(x => x.pics_big),
        current
      })
    },
    async chooseAddress() {
      const res = await wepy.chooseAddress().catch(err => err)
      if (res.errMsg !== 'chooseAddress:ok') {
        return wepy.showToast({
          title: '获取收货地址失败',
          icon: 'none'
        })
      }
      this.address = res
      wepy.setStorageSync('address', res)
      this.$apply()
    },
    // 点击加入购物车
    addCart() {
      const { goods_id, goods_name, goods_price, goods_big_logo } = this.detailList
      this.$parent.addCart(goods_id, goods_name, goods_price, goods_big_logo)
    }
  }
  onLoad(options) {
    this.goods_id = options.goods_id
    this.getGoodsDetail()
  }
  async getGoodsDetail() {
    const { data: res } = await wepy.get('/goods/detail?goods_id=' + this.goods_id)
    wepy.baseToast(res)
    res.message.goods_introduce = res.message.goods_introduce.replace(/.wepy/g, '.jpg')
    this.detailList = res.message
    this.$apply()
  }
}
